home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / UPC12BS1.ZIP / LIB / HOSTABLE.C < prev    next >
C/C++ Source or Header  |  1993-06-21  |  28KB  |  701 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       h o s t a b l e . c                                          */
  3. /*                                                                    */
  4. /*       Remote host table routines for UUPC/extended                 */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1990-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19.  /*
  20.   *      $Id: hostable.c 1.7 1993/06/21 02:17:31 ahd Exp $
  21.   *
  22.   *      $Log: hostable.c $
  23.  *     Revision 1.7  1993/06/21  02:17:31  ahd
  24.  *     Correct errors in mail routing via HOSTPATH
  25.  *
  26.  *     Revision 1.6  1993/05/29  15:19:59  ahd
  27.  *     Allow configured systems, passwd files
  28.  *
  29.  *     Revision 1.5  1993/04/11  00:32:29  ahd
  30.  *     Global edits for year, TEXT, etc.
  31.  *
  32.  *     Revision 1.4  1993/04/04  04:57:01  ahd
  33.  *     Trap existence of local host name in SYSTEMS file
  34.  *
  35.  *     Revision 1.3  1992/12/18  12:05:57  ahd
  36.  *     Suppress duplicate machine state messages to improving OS/2 scrolling
  37.  *
  38.  *     Revision 1.3  1992/12/18  12:05:57  ahd
  39.  *     Suppress duplicate machine state messages to improving OS/2 scrolling
  40.  *
  41.  *    Revision 1.2  1992/11/22  20:58:55  ahd
  42.  *    Use strpool to allocate const strings
  43.  *
  44.  *    18 Mar 1990 Create hostable.c from router.c                    ahd
  45.  *                Move code to generate localdomain to here          ahd
  46.  *    22 Apr 90   Perform check for full host name before examining
  47.  *                name without domain.                               ahd
  48.  *    29 Jul 90   Only load host table based on first six characters
  49.  *                of host name.                                      ahd
  50.  *
  51.   */
  52.  
  53. #include <ctype.h>
  54. #include <limits.h>
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <sys/types.h>
  59.  
  60. #include "lib.h"
  61. #include "hlib.h"
  62. #include "hostable.h"
  63. #include "security.h"
  64.  
  65. currentfile();
  66.  
  67. static struct HostTable *hosts = NULL;
  68.  
  69. static size_t  HostElements = 0;
  70.  
  71. static size_t loadhost( void );
  72.  
  73. static int hostcmp( const void *a , const void *b );
  74.  
  75. static size_t localdomainl;   /* Length of localdomain               */
  76.  
  77. /*--------------------------------------------------------------------*/
  78. /*    c h e c k n a m e                                               */
  79. /*                                                                    */
  80. /*    Perform a search for a single host name                         */
  81. /*                                                                    */
  82. /*    Rewritten for release 1.10a.  The old release had               */
  83. /*    most of the same logic, but nested it inside the search         */
  84. /*    loop; adding new cases (specifically, the wildcard domain       */
  85. /*    search) was difficult.  This version is slower because it       */
  86. /*    makes multiple passes through the host table, but this          */
  87. /*    isn't really performance code for a small (under 100 hosts)     */
  88. /*    table.                                 ahd 26 April 1991        */
  89. /*                                                                    */
  90. /*    Note because we save the arguments and use a static variable    */
  91. /*    to save the result of searches, this function is not            */
  92. /*    recursive!                                                      */
  93. /*--------------------------------------------------------------------*/
  94.  
  95. struct HostTable *checkname(const char *name)
  96. {
  97.    char  hostname[MAXADDR];   /* Local copy of name to process       */
  98.    char *period;              /* Pointer "." in hostname             */
  99.    size_t namel;              /* Length of the name input            */
  100.    size_t column;             /* Length of the name input            */
  101.  
  102.    static char savename[MAXADDR] = "";
  103.                               /* Saved copy of name to make function
  104.                                  reducible                           */
  105.    static struct HostTable *hostz;
  106.  
  107. /*--------------------------------------------------------------------*/
  108. /*                       Validate the argument                        */
  109. /*--------------------------------------------------------------------*/
  110.  
  111.    if ((name == NULL) || ((namel = strlen(name)) == 0))
  112.    {
  113.       printmsg(0,"checkname: Invalid (missing) hostname passed");
  114.       panic();
  115.       return NULL;           /* Never executed                      */
  116.    }
  117.  
  118. /*--------------------------------------------------------------------*/
  119. /*    If same argument as last time, return same result; otherwise    */
  120. /*    save input for next pass                                        */
  121. /*--------------------------------------------------------------------*/
  122.  
  123.    if (equali(name, savename))
  124.       return hostz;
  125.    strcpy( savename, name);   /* Save for next pass                  */
  126.  
  127. /*--------------------------------------------------------------------*/
  128. /*                      Search for the full name                      */
  129. /*--------------------------------------------------------------------*/
  130.  
  131.    if ((hostz = searchname(name, MAXADDR)) != BADHOST)
  132.       return hostz;
  133.  
  134. /*--------------------------------------------------------------------*/
  135. /*    If the name already has the local domain attached, search for   */
  136. /*    the host name without the domain.                               */
  137. /*--------------------------------------------------------------------*/
  138.  
  139.    column = namel - localdomainl;
  140.    if ((namel > localdomainl) && equali(E_localdomain, &name[column]) &&
  141.        (name[ column - 1] == '.'))
  142.    {
  143.       if ((hostz = searchname(name,column-1 )) != BADHOST)
  144.          return hostz;
  145.    } /* if */
  146.  
  147. /*--------------------------------------------------------------------*/
  148. /*    If the name already has the UUCP  domain attached, search for   */
  149. /*    the host name without the domain.                               */
  150. /*--------------------------------------------------------------------*/
  151.  
  152.    column = namel - 5;
  153.    if ((column > 0) && equali(".UUCP", &name[column]))
  154.    {
  155.       if ((hostz = searchname(name, column )) != BADHOST)
  156.          return hostz;
  157.    } /* if */
  158.  
  159. /*--------------------------------------------------------------------*/
  160. /*              Search for the name in the local domain               */
  161. /*--------------------------------------------------------------------*/
  162.  
  163.    if ((namel + localdomainl + 2) < MAXADDR)
  164.    {
  165.       sprintf(hostname,"%s.%s",name,E_localdomain);
  166.       if ((hostz = searchname(hostname, MAXADDR)) != BADHOST)
  167.          return hostz;
  168.    } /* if */
  169.  
  170. /*--------------------------------------------------------------------*/
  171. /*    If a simple name and not found, return search for truncated     */
  172. /*    UNIX name.                                                      */
  173. /*--------------------------------------------------------------------*/
  174.  
  175.    if ( strchr(name,'.') == NULL )
  176.       return checkreal( name );
  177.  
  178. /*--------------------------------------------------------------------*/
  179. /*               Perform a wildcard domain name search                */
  180. /*--------------------------------------------------------------------*/
  181.  
  182.    period = (char *) name;    /* Begin at front of name              */
  183.    while( period != NULL )
  184.    {
  185.       sprintf( hostname,(*period == '.') ? "*%s" : "*.%s",period);
  186.                               /* We add the missing